<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 6] 6.9 The continue Statement</TITLE>
<META NAME="author" CONTENT="Mark Grand">
<META NAME="date" CONTENT="Thu Jul 31 13:14:39 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="java">
<META NAME="title" CONTENT="Java Language Reference">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java Language Reference" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch06_08.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 6<br>Statements and Control Structures</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch06_10.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JLR2-CH-6-SECT-9">6.9 The continue Statement</A></h2>

<P CLASS=para>
<A NAME="CH06.CONTINUE1"></A><A NAME="CH06.CONTINUE2"></A><A NAME="CH06.CONTINUE3"></A>A <tt CLASS=literal>continue</tt> statement stops
the current iteration of an iteration statement and transfers control
to the start of the next iteration:


<p>
<img align=middle src="./figs/jlr0618.gif" alt="[Graphic: Figure from the text]" width=424 height=49 border=0>

<P CLASS=para>
A <tt CLASS=literal>continue</tt> statement must occur within
a <tt CLASS=literal>while</tt>,
<tt CLASS=literal>for</tt>, or <tt CLASS=literal>do</tt> statement or the
compiler issues an error message.

<P CLASS=para>
If a <tt CLASS=literal>continue</tt> statement does not contain
an identifier, the statement stops the current iteration in the
innermost enclosing <tt CLASS=literal>while</tt>, <tt CLASS=literal>for</tt>,
or <tt CLASS=literal>do</tt> statement and attempts to transfer control
to the start of the next iteration. This means that in a
<tt CLASS=literal>while</tt> or <tt CLASS=literal>do</tt> statement, the
<tt CLASS=literal>continue</tt> statement transfers
control to just after the contained statement of
the <tt CLASS=literal>while</tt> or <tt CLASS=literal>do</tt> statement.
In a <tt CLASS=literal>for</tt> statement, the <tt CLASS=literal>continue</tt>
statement transfers control to the increment portion of the
<tt CLASS=literal>for</tt> statement.
Here is an example of a <tt CLASS=literal>continue</tt> statement that
contains no identifier:

<DIV CLASS=screen>
<P>
<PRE>
public static void main (String[] argv) {
    for (int i=0; i&lt;=15; i++) {
        System.out.println(i);
        if ( (i&amp;1) == 0 )
            continue;
        System.out.println("That's odd");
    }
}
</PRE>
</DIV>

<P CLASS=para>
The above example outputs the numbers 0 through 15, printing
"That's odd" after each odd number.

<P CLASS=para>
The innermost <tt CLASS=literal>while</tt>, <tt CLASS=literal>for</tt>, 
<tt CLASS=literal>do</tt>, or <tt CLASS=literal>switch</tt> statement that
encloses the <tt CLASS=literal>continue</tt> statement must be in the 
immediately enclosing method or initializer block. 
The <tt CLASS=literal>continue</tt> statement 
in the following example is used incorrectly and generates an error:

<DIV CLASS=screen>
<P>
<PRE>
while (true) {
    class X {
        void doIt() {
            continue;
        } 
    } 
    new X().doIt();
} 
</PRE>
</DIV>

<P CLASS=para>
If a <tt CLASS=literal>continue</tt> statement contains an identifier,
the identifier must
be defined as the label of an enclosing <tt CLASS=literal>while</tt>,
<tt CLASS=literal>for</tt>, or <tt CLASS=literal>do</tt> statement. A
<tt CLASS=literal>continue</tt> statement that contains an identifier
stops the current
iteration of the labeled iteration statement and attempts to transfer
control to the start of the next iteration of that loop. Here is
an example of a <tt CLASS=literal>continue</tt> statement that contains
an identifier:

<DIV CLASS=screen>
<P>
<PRE>
public boolean search(int x, int a[][]) {
    int count = 0;
  top:
    for (int i=0; i&lt;a.length; i++) {
        int b[] = a[i];
        for (int j=0; j &lt; b.length; j++) {
            if (x == b[j])
                return true;
            if ( x &lt; b[j])
                continue top;
        } // for j
        count++;
        if (count &gt; 100)
            return false;
    } // for i
    return false;
} // search()
</PRE>
</DIV>

<P CLASS=para>
The above method searches an array
of arrays of integers for a specified value. The method assumes
that the values in the sub-arrays are in descending order. The method
gives up after checking 100 values.

<P CLASS=para>
The label used in a <tt CLASS=literal>continue</tt> statement must be in 
the immediately enclosing method or initializer block. 

<P CLASS=para>
The statement to
which a <tt CLASS=literal>continue</tt> statement attempts to transfer
control is called the target statement. If a <tt CLASS=literal>continue</tt>
statement occurs inside a <tt CLASS=literal>try</tt> statement, control
may not immediately transfer to the target statement. If a
<tt CLASS=literal>try</tt> statement has a <tt CLASS=literal>finally</tt>
clause, the <tt CLASS=literal>finally</tt>
block is executed before control leaves the <tt CLASS=literal>try</tt>
statement for any reason. This means that if a <tt CLASS=literal>continue</tt>
statement occurs inside a <tt CLASS=literal>try</tt> statement (but
not in its <tt CLASS=literal>finally</tt> block) and the target statement
is outside of the <tt CLASS=literal>try</tt> statement, the
<tt CLASS=literal>finally</tt> block is executed first, before the control
transfer can take place.

<P CLASS=para>
If the <tt CLASS=literal>finally</tt> block contains a
<tt CLASS=literal>break</tt>, <tt CLASS=literal>continue</tt>,
<tt CLASS=literal>return</tt>, or <tt CLASS=literal>throw</tt> statement, the
pending control transfer
for the previously executed <tt CLASS=literal>continue</tt> statement
is forgotten. Instead, control is transferred to the target of the
<tt CLASS=literal>break</tt>, <tt CLASS=literal>continue</tt>,
<tt CLASS=literal>return</tt>, or <tt CLASS=literal>throw</tt> statement in the
<tt CLASS=literal>finally</tt> block.

<P CLASS=para>
If the <tt CLASS=literal>finally</tt> block does not contain a
<tt CLASS=literal>break</tt>, <tt CLASS=literal>continue</tt>,
<tt CLASS=literal>return</tt>, or <tt CLASS=literal>throw</tt> statement, the
pending control transfer
happens after the <tt CLASS=literal>finally</tt> block is done executing,
unless the target statement is enclosed by another
<tt CLASS=literal>try</tt> statement. If there is another enclosing
<tt CLASS=literal>try</tt> statement and it has a <tt CLASS=literal>finally</tt>
clause, that <tt CLASS=literal>finally</tt> block is also executed before the
control transfer can take place. Execution proceeds in this manner
until the target statement of the <tt CLASS=literal>continue</tt> is
executed.

<P CLASS=para>
<b>References</b>
<A HREF="ch02_02.htm#JLR2-CH-2-SECT-2.1">Identifiers</A>;
<A HREF="ch06_02.htm#JLR2-CH-6-SECT-2">Labeled Statements</A>;
<A HREF="ch06_08.htm#JLR2-CH-6-SECT-8">The break Statement</A>;
<A HREF="ch06_07.htm#JLR2-CH-6-SECT-7.2">The do Statement</A>;
<A HREF="ch06_07.htm#JLR2-CH-6-SECT-7.3">The for Statement</A>;
<A HREF="ch06_10.htm#JLR2-CH-6-SECT-10">The return Statement</A>;
<A HREF="ch06_11.htm#JLR2-CH-6-SECT-11">The throw Statement</A>;
<A HREF="ch06_12.htm#JLR2-CH-6-SECT-12">The try Statement</A>;
<A HREF="ch06_07.htm#JLR2-CH-6-SECT-7.1">The while Statement</A>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch06_08.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch06_10.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>The break Statement</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>The return Statement</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
